import { shapes, Util } from "./Util";
import { ShapeTypes } from "./types/ShapeTypes";
import { Node } from "./Node";

export class Shape<Types extends ShapeTypes = ShapeTypes> extends Node<Types> {
  colorKey: string;

  constructor(types: Types) {
    super(types);
    let key = Util.getColorKey();
    this.colorKey = key;
    shapes[key] = this;
  }

  drawScene(): void {
    const layer = this.getLayer();
    const context = layer.getContext();
    const drawFunc = this.getSceneFunc();

    context.save();

    let at = this.getAbsoluteTransform().getMatrix()
    context.transform(at[0], at[1], at[2], at[3], at[4], at[5])

    drawFunc.call(this, context);
    context.restore();
  }

  drawHit(): void {
    let layer = this.getLayer();
    let context = layer.getHitContext();
    const drawFunc = this.getHitFunc() || this.getSceneFunc();

    context.save();

    let at = this.getAbsoluteTransform().getMatrix()
    context.transform(at[0], at[1], at[2], at[3], at[4], at[5])

    drawFunc.call(this, context);
    context.restore();
  }

  getSceneFunc() {
    return this["_sceneFunc"];
  }

  getHitFunc() {
    return this["_sceneHit"];
  }
}
